/*
 *
 * Copyright 2011-2012 University of Illinois
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

Components.utils.import("resource://oaa-cache-inspector/cache-inspector-common.js");

/**
 * @function setDefaultPreferences
 *
 * @memberOf OAA_CI_UTILS
 *
 * @desc Sets preferences to default values
 */


OAA_CI_UTILS.setDefaultPreferences = function () {

  var preferences = {};
  
  preferences.ruleset_id   = 'WCAG20_ARIA_TRANS';                              
  
  preferences.wcag20_level = 3;                              
    
  preferences.broken_links = false;    

  preferences.show_menu = true;
  
  OAA_CI_UTILS.setPreferences(preferences);
  
  return preferences;
}

/**
 * @function setDefaultPreferences
 *
 * @memberOf OAA_CI_UTILS
 *
 * @desc Returns preferences object with current preferences 
 *
 * @return {Object}  Object with properties representing preferences  
 */


OAA_CI_UTILS.getPreferences = function () {

  var preferences = {};
  
  var pref_manager = Components.classes["@mozilla.org/preferences-service;1"]
                                .getService(Components.interfaces.nsIPrefBranch);            
                                
  try {
    preferences.show_menu = pref_manager.getBoolPref('extensions.oaa-accessibility-evaluator.showMenu');                              
  } catch (e) {
    preferences = OAA_CI_UTILS.setDefaultPreferences();
  }


  try {
    preferences.ruleset_id = pref_manager.getCharPref('extensions.oaa-accessibility-evaluator.rulesetId');                              
  
    preferences.wcag20_level = pref_manager.getIntPref('extensions.oaa-accessibility-evaluator.wcag20Level');                              
    
    preferences.broken_links = pref_manager.getBoolPref('extensions.oaa-accessibility-evaluator.brokenLinks');    
  } catch(e) {
    preferences = OAA_CI_UTILS.setDefaultPreferences();
  }

  var menu = window.document.getElementById('ID_MENU_A11Y_EVALUATOR');
  
  if (menu) {
    menu.collapsed = !preferences.show_menu; 
  }

  return preferences;
};


OAA_CI_UTILS.setPreferences = function (preferences) {

  OAA_CI_UTILS.console("Set Preferences:");
  OAA_CI_UTILS.console("   Ruleset ID: " + preferences.ruleset_id);
  OAA_CI_UTILS.console("   WCAG Level: " + preferences.wcag20_level);
  OAA_CI_UTILS.console("  Broken Link: " + preferences.broken_links);
  OAA_CI_UTILS.console("    Show Menu: " + preferences.show_menu);

  var pref_manager = Components.classes["@mozilla.org/preferences-service;1"]
                                .getService(Components.interfaces.nsIPrefBranch);                           
  
  if (typeof preferences.ruleset_id == "string")   pref_manager.setCharPref('extensions.oaa-accessibility-evaluator.rulesetId',   preferences.ruleset_id );                              
  if (typeof preferences.wcag20_level == "number") pref_manager.setIntPref('extensions.oaa-accessibility-evaluator.wcag20Level',  preferences.wcag20_level);                              
  if (typeof preferences.broken_links == "boolean") pref_manager.setBoolPref('extensions.oaa-accessibility-evaluator.brokenLinks', preferences.broken_links);    

  if (typeof preferences.show_menu == "boolean") pref_manager.setBoolPref('extensions.oaa-accessibility-evaluator.showMenu', preferences.show_menu);                              

  var menu = window.document.getElementById('ID_MENU_A11Y_EVALUATOR');
  
  if (menu) {
    menu.collapsed = !preferences.show_menu; 
  }


};

